<?php
/**
 * Created by PhpStorm.
 * User: Leslie
 * Date: 2016/7/7
 * Time: 15:38
 */

namespace app\models;


class Invocie
{

    /**
     * 发票总数
     * @param int $startTime
     * @param int $endTime
     * @param $status
     * @param $filter
     * @param $institutionId
     * @return mixed
     */
    public static function getInvoiceListTotal($startTime = 0, $endTime = 0, $status, $filter, $institutionId)
    {
        $filterSQL = '1= 1';

        if ($institutionId != '0') {
            $filterSQL = $filterSQL . ' and (institution_code=:institutionId or parent_code=:institutionId)';
        }
        if ($status != '99') {
            $filterSQL = $filterSQL . ' and (audit_status=:status)';
        }
        if ($filter != '0') {
            $filterSQL = $filterSQL . ' and (pk_code like :filter or institution_name like :filter)';
        }

        if ($startTime != '0') {
            $filterSQL = $filterSQL . ' and (applay_time>= :startTime)';
        }
        if ($endTime != '0') {
            $filterSQL = $filterSQL . ' and (SUBSTRING(applay_time FROM 1 FOR 10)<= :endTime)';
        }
        $sql = "SELECT count(*) AS totalnum ,sum(p_money) AS totalamount FROM v_invoice WHERE
                 $filterSQL";

        $cmd = \Yii::$app->db->createCommand($sql);

        if ($institutionId != '0') {
            $cmd->bindValue(':institutionId', $institutionId);
        }
        if ($status != '99') {
            $cmd->bindValue(':status', $status);
        }
        if ($filter != '0') {
            $filter = '%' . $filter . '%';
            $cmd->bindValue(':filter', $filter);
        }
        if ($startTime != '0') {
            $cmd->bindValue(':startTime', $startTime);
        }
        if ($endTime != '0') {
            $cmd->bindValue(':endTime', $endTime);
        }
        $result = $cmd->queryOne();

        return $result['totalnum'];
    }

    /**
     * 已开发票总额
     * @param $institutionId
     * @return int
     */
    public static function getInvoiced($institutionId)
    {
        $sql = "SELECT count(*) AS totalnum ,sum(p_money) AS totalamount FROM v_invoice WHERE audit_status=1 AND
                 institution_code=:institutionId";

        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':institutionId', $institutionId);
        $result = $cmd->queryOne();

        if ($result['totalnum'] == '0') {
            $result['totalamount'] = 0;
        }
        return $result['totalamount'];
    }

    /**
     * 发票列表
     * @param $start
     * @param $pagesize
     * @param int $startTime
     * @param int $endTime
     * @param $status
     * @param $filter
     * @param $institutionId
     * @return array
     */
    public static function getInvoiceList($start, $pagesize, $startTime = 0, $endTime = 0, $status, $filter, $institutionId)
    {
        $filterSQL = '1= 1';

        if ($institutionId != '0') {
            $filterSQL = $filterSQL . ' and (institution_code=:institutionId or parent_code=:institutionId)';
        }
        if ($status != '99') {
            $filterSQL = $filterSQL . ' and (audit_status=:status)';
        }
        if ($filter != '0') {
            $filterSQL = $filterSQL . ' and (pk_code like :filter or institution_name like :filter)';
        }

        if ($startTime != '0') {
            $filterSQL = $filterSQL . ' and (applay_time>= :startTime)';
        }
        if ($endTime != '0') {
            $filterSQL = $filterSQL . ' and (SUBSTRING(applay_time FROM 1 FOR 10)<= :endTime)';
        }
        $sql = "SELECT *  FROM v_invoice WHERE
                 $filterSQL LIMIT $start,$pagesize";

        $cmd = \Yii::$app->db->createCommand($sql);

        if ($institutionId != '0') {
            $cmd->bindValue(':institutionId', $institutionId);
        }
        if ($status != '99') {
            $cmd->bindValue(':status', $status);
        }
        if ($filter != '0') {
            $filter = '%' . $filter . '%';
            $cmd->bindValue(':filter', $filter);
        }
        if ($startTime != '0') {
            $cmd->bindValue(':startTime', $startTime);
        }
        if ($endTime != '0') {
            $cmd->bindValue(':endTime', $endTime);
        }
        $result = $cmd->queryAll();

        return $result;
    }

    /**
     * 发票详情
     * @param $invId
     * @return array|false
     */
    public static function getInvoiceDetail($invId)
    {
        $sql = "SELECT * FROM v_invoice WHERE id= :invId";
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':invId', $invId);
        $result = $cmd->queryOne();

        return $result;
    }

    /**
     * 发票审核删除
     * @param $id
     * @param $flag
     * @param $auditRemark
     * @param $userId
     * @return false|null|string
     */
    public static function setInvoiceStatus($id, $flag, $auditRemark, $userId)
    {
        $sql = "call p_audit_delete_invoice(:in_id,:in_flag,:in_auditRemark,:in_userId)";
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':in_id', $id);
        $cmd->bindValue(':in_flag', $flag);
        $cmd->bindValue(':in_auditRemark', $auditRemark);
        $cmd->bindValue(':in_userId', $userId);
        $result = $cmd->queryScalar();

        return $result;
    }

    /**
     * 发票申请新增
     * @param $institution_code
     * @param $invoice_head
     * @param $fp_date
     * @param $invoice_type
     * @param $category_name
     * @param $unit
     * @param $pcount
     * @param $p_money
     * @param $remark
     * @param $applay_user
     * @return false|null|string
     */
    public static function saveInvoice($institution_code, $invoice_head, $fp_date, $invoice_type, $category_name, $unit, $pcount, $p_money, $remark, $applay_user)
    {
        $sql = "call p_save_invoice(:in_institution_code,:in_invoice_head,:in_fp_date,:in_invoice_type,:in_category_name,:in_unit,:in_pcount,:in_p_money,:in_remark,:in_applay_user)";
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':in_institution_code', $institution_code);
        $cmd->bindValue(':in_invoice_head', $invoice_head);
        $cmd->bindValue(':in_fp_date', $fp_date);
        $cmd->bindValue(':in_invoice_type', $invoice_type);
        $cmd->bindValue(':in_category_name', $category_name);
        $cmd->bindValue(':in_unit', $unit);
        $cmd->bindValue(':in_pcount', $pcount);
        $cmd->bindValue(':in_p_money', $p_money);
        $cmd->bindValue(':in_remark', $remark);
        $cmd->bindValue(':in_applay_user', $applay_user);
        $result = $cmd->queryScalar();

        return $result;

    }

}